/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.controller.cp;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import vn.webapp.controller.BaseWeb;
import vn.webapp.dto.DataPage;
import vn.webapp.form.StudentValidationForm;
import vn.webapp.model.Classes;
import vn.webapp.model.Students;
import vn.webapp.service.StudentsService;
import vn.webapp.service.ClassesService;
/**
 *
 * @author Tonytran
 */
@Controller("cpStudents")
@RequestMapping(value = {"/cp"})
public class StudentsController extends BaseWeb{
    
    @Autowired
    private StudentsService studentsService;
    
    @Autowired
    private ClassesService classesService;
    
    private int nav = 4;
    /**
     * 
     * @param model
     * @return 
     */
    @RequestMapping(value = "/students" , method = RequestMethod.GET)
    public String classes(ModelMap model, HttpSession session,
    @RequestParam(value = "page", defaultValue = "0") int page){
        int pageSize = 10;        
        DataPage<Students> dataStudents = studentsService.filter(page, pageSize);
        //DataPage<Students> dataStudents = studentsService.list();
        model.put("sz_Response", session.getAttribute("response"));
        session.setAttribute("response", "");
        model.put("dataStudents", dataStudents);
        model.put("pageIndex", page);
        model.put("nav", this.nav);
        return "cp.students";
    }
    
    /**
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/studentdetail/{id}")
    public String viewDetail(ModelMap model, @PathVariable("id") int id) {
        Students student = studentsService.viewDetail(id);
        DataPage<Classes> dataClasses = classesService.list();
        model.put("studentForm", new StudentValidationForm());
        model.put("dataClasses", dataClasses);
        model.put("dataStudent", student);
        model.put("nav", this.nav);
        return "cp.studentdetail";
    }
    

    @RequestMapping(value = "/savestudentdetail", method = RequestMethod.POST)
    public String processEditStudent(@Valid @ModelAttribute("studentForm") StudentValidationForm studentForm,
            BindingResult result, HttpServletRequest request, Map model, HttpSession session) {
        
        // Add the saved validationForm to the model  
        model.put("nav", this.nav);
        if (result.hasErrors()) {
            Students student = new Students();
            student.setClass_id(studentForm.getClassId());
            DataPage<Classes> dataClasses = classesService.list();
            model.put("dataClasses", dataClasses);
            model.put("dataStudent", student);
            return "cp.studentdetail";
        }else
        {
            String StudentName = studentForm.getName();
            String StudentCode = studentForm.getCode();
            int StudentId = studentForm.getId();
            int ClassId = studentForm.getClassId();
            Students o_Student = studentsService.checkExistsCode(StudentCode, StudentId);
            if(o_Student != null)
            {
                model.put("sz_Response", "The code "+StudentCode+" is being used.");
                return "cp.studentdetail";
            }else{
                int i_UpdateStudent = studentsService.updateTheStudent(StudentCode, StudentName, ClassId, StudentId);
                session.setAttribute("response", "Edit successfully!");
                return "redirect:" + this.baseUrl + "/cp/students.html";
            }
        }
    }
    
    @RequestMapping(value = "/removestudent/{id}", method = RequestMethod.GET)
    public String processRemoveStudent(ModelMap model, @PathVariable("id") int id, HttpSession session) {
        int i_IsRemoved = studentsService.removeStudent(id);
        String sz_Response;
        if (i_IsRemoved != 0) {
            sz_Response = "Remove the student successfully !";
        } else {
            sz_Response = "Remove the student not successfully. Please retry";
        }
        session.setAttribute("response", sz_Response);
        return "redirect:" + this.baseUrl + "/cp/students.html";
    }
    
    @RequestMapping(value = "/addnewstudent", method = RequestMethod.GET)
    public String addnewstudent(Map model){
        model.put("nav", this.nav);
        model.put("studentFormAdd", new StudentValidationForm());
        DataPage<Classes> dataClasses = classesService.list();
        model.put("dataClasses", dataClasses);
        return "cp.addnewstudent";
    }
    
    @RequestMapping(value = "/savestudent", method = RequestMethod.POST)
    public String processAddNewStudent(@Valid @ModelAttribute("studentFormAdd") StudentValidationForm studentFormAdd,
            BindingResult result, Map model, HttpSession session) {
        
        // Add the saved validationForm to the model  
        model.put("nav", this.nav);
        if (result.hasErrors()) {
            DataPage<Classes> dataClasses = classesService.list();
            model.put("dataClasses", dataClasses);
            return "cp.addnewstudent";
        }else
        {
            String StudentName = studentFormAdd.getName();
            String StudentCode = studentFormAdd.getCode();
            int ClassId = studentFormAdd.getClassId();
            int StudentId = studentFormAdd.getId();
            Students o_Student = studentsService.checkExistsCode(StudentCode, StudentId);
            if(o_Student != null)
            {
                model.put("sz_Response", "The code "+StudentCode+" is being used.");
                return "cp.addnewstudent";
            }else
            {
                int i_InsertAStudent = studentsService.saveAStudent(StudentCode, StudentName, ClassId);
                session.setAttribute("response", "Add successfully!");
                return "redirect:" + this.baseUrl + "/cp/students.html";
            }
        }
    }
}
